package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsSumstocktransferbillentry;
import com.ruoyi.basebill.mapper.OmsSumstocktransferbillMapper;
import com.ruoyi.basebill.domain.OmsSumstocktransferbill;
import com.ruoyi.basebill.service.IOmsSumstocktransferbillService;

/**
 * 汇总库存调拨单Service业务层处理
 * 
 * @author wenyixiong
 * @date 2023-02-18
 */
@Service
public class OmsSumstocktransferbillServiceImpl implements IOmsSumstocktransferbillService 
{
    @Autowired
    private OmsSumstocktransferbillMapper omsSumstocktransferbillMapper;
    @Autowired
    private OmsSumstocktransferbillServiceImpl sumstocktransferbillService;

    /**
     * 查询汇总库存调拨单
     * 
     * @param id 汇总库存调拨单主键
     * @return 汇总库存调拨单
     */
    @Override
    public OmsSumstocktransferbill selectOmsSumstocktransferbillById(Long id)
    {
        return omsSumstocktransferbillMapper.selectOmsSumstocktransferbillById(id);
    }

    /**
     * 查询汇总库存调拨单列表
     * 
     * @param omsSumstocktransferbill 汇总库存调拨单
     * @return 汇总库存调拨单
     */
    @Override
    public List<OmsSumstocktransferbill> selectOmsSumstocktransferbillList(OmsSumstocktransferbill omsSumstocktransferbill)
    {
        return omsSumstocktransferbillMapper.selectOmsSumstocktransferbillList(omsSumstocktransferbill);
    }

    @Override
    public List<OmsSumstocktransferbill> selectOmsSumstocktransferbillDetailList(OmsSumstocktransferbill omsSumstocktransferbill) {
        return omsSumstocktransferbillMapper.selectOmsSumstocktransferbillDetailList(omsSumstocktransferbill);
    }

    /**
     * 新增汇总库存调拨单
     * 
     * @param omsSumstocktransferbill 汇总库存调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSumstocktransferbill(OmsSumstocktransferbill omsSumstocktransferbill)
    {
        int rows = omsSumstocktransferbillMapper.insertOmsSumstocktransferbill(omsSumstocktransferbill);
        sumstocktransferbillService.insertOmsSumstocktransferbillentry(omsSumstocktransferbill);
        return rows;
    }

    /**
     * 修改汇总库存调拨单
     * 
     * @param omsSumstocktransferbill 汇总库存调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSumstocktransferbill(OmsSumstocktransferbill omsSumstocktransferbill)
    {
        //传入汇总库存调拨单不存在分录则不用修改分录信息只修改表头信息
        if(omsSumstocktransferbill.getOmsSumstocktransferbillentryList() != null && omsSumstocktransferbill.getOmsSumstocktransferbillentryList().size() != 0) {
            omsSumstocktransferbillMapper.deleteOmsSumstocktransferbillentryByParentid(omsSumstocktransferbill.getId());
            sumstocktransferbillService.insertOmsSumstocktransferbillentry(omsSumstocktransferbill);
        }
        return omsSumstocktransferbillMapper.updateOmsSumstocktransferbill(omsSumstocktransferbill);
    }

    /**
     * 批量删除汇总库存调拨单
     * 
     * @param ids 需要删除的汇总库存调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSumstocktransferbillByIds(Long[] ids)
    {
        omsSumstocktransferbillMapper.deleteOmsSumstocktransferbillentryByParentids(ids);
        return omsSumstocktransferbillMapper.deleteOmsSumstocktransferbillByIds(ids);
    }

    /**
     * 删除汇总库存调拨单信息
     * 
     * @param id 汇总库存调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSumstocktransferbillById(Long id)
    {
        omsSumstocktransferbillMapper.deleteOmsSumstocktransferbillentryByParentid(id);
        return omsSumstocktransferbillMapper.deleteOmsSumstocktransferbillById(id);
    }

    /**
     * 新增汇总库存调拨单分录信息
     * 
     * @param omsSumstocktransferbill 汇总库存调拨单对象
     */
    public void insertOmsSumstocktransferbillentry(OmsSumstocktransferbill omsSumstocktransferbill)
    {
        List<OmsSumstocktransferbillentry> omsSumstocktransferbillentryList = omsSumstocktransferbill.getOmsSumstocktransferbillentryList();
        Long id = omsSumstocktransferbill.getId();
        if (StringUtils.isNotNull(omsSumstocktransferbillentryList))
        {
            List<OmsSumstocktransferbillentry> list = new ArrayList<OmsSumstocktransferbillentry>();
            for (OmsSumstocktransferbillentry omsSumstocktransferbillentry : omsSumstocktransferbillentryList)
            {
                omsSumstocktransferbillentry.setParentid(id);
                list.add(omsSumstocktransferbillentry);
            }
            if (list.size() > 0)
            {
                omsSumstocktransferbillMapper.batchOmsSumstocktransferbillentry(list);
            }
        }
    }

    /**
     * 查询是否推送过EAS的汇总库存调拨单数据
     * @param isTrans
     * @return
     */
    public List<OmsSumstocktransferbill> selectPushEASSumStockTransferbillList(int isTrans){
        return omsSumstocktransferbillMapper.selectPushEASSumStockTransferbillList(isTrans);
    }
}
